package September29;

import java.io.*;
import java.util.*;

public class Supermarket implements Serializable {
    private static final long serialVersionUID = 1L;
    private Map<String, Member> members = new HashMap<>();

    public static void main(String[] args) {
        Supermarket supermarket = loadSupermarket();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：1. 开卡 2. 查询积分 3. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            switch (choice) {
                case 1:
                    System.out.println("输入卡号：");
                    String cardNumber = scanner.nextLine();
                    System.out.println("输入姓名：");
                    String name = scanner.nextLine();
                    supermarket.addMember(new Member(cardNumber, name));
                    break;
                case 2:
                    System.out.println("输入卡号：");
                    cardNumber = scanner.nextLine();
                    Member member = supermarket.getMember(cardNumber);
                    if (member != null) {
                        System.out.println("会员 " + member.getName() + " 的积分为：" + member.getPoints());
                    } else {
                        System.out.println("未找到该会员");
                    }
                    break;
                case 3:
                    System.out.println("正在退出...");
                    saveSupermarket(supermarket);
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选项，请重新选择。");
                    break;
            }
        }
    }

    public void addMember(Member member) {
        members.put(member.getCardNumber(), member);
    }

    public Member getMember(String cardNumber) {
        return members.get(cardNumber);
    }

    public static Supermarket loadSupermarket() {
        File file = new File("supermarket.ser");
        if (file.exists()) {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                return (Supermarket) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return new Supermarket();
    }

    public static void saveSupermarket(Supermarket supermarket) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("supermarket.ser"))) {
            oos.writeObject(supermarket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Member implements Serializable {
    private static final long serialVersionUID = 1L;
    private String cardNumber;
    private String name;
    private int points;

    public Member(String cardNumber, String name) {
        this.cardNumber = cardNumber;
        this.name = name;
        this.points = 0;
    }

    // Getters and setters
    public String getCardNumber() {
        return cardNumber;
    }

    public String getName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    public void addPoints(int points) {
        this.points += points;
    }

    public void exchangePoints(int points) {
        if (this.points >= points) {
            this.points -= points;
        } else {
            System.out.println("积分不足");
        }
    }

    public void setPassword(String password) {
        // 实现密码设置逻辑
    }
}
